import UIKit

//https://www.jianshu.com/p/7845e9af4e97

let str = #"This is also a Swift string literal"#
// before
let rain = "The \"rain\" in \"Spain\" falls \\mainly on the Spaniards."
// after
let rain1 = #"The "rain" in "Spain" falls \mainly on the Spaniards."#
let answer = 42
// before
let dontpanic = "The answer to life, the universe, and everything is \(answer)"
print(dontpanic)
// after
let dontpanic1 = #"The answer to life, the universe, and everything is \#(answer)"#
print(dontpanic1)

let str1 = ##"My dog said "woof"#gooddog"##
let multiline = #"""
  The answer to life,
  the universe,
  and everything is \#(answer).
"""#
print(multiline)


// before
let regex1 = "\\\\[A-Z]+[A-Za-z]+\\.[a-z]+"
// after
let regex2 = #"\\[A-Z]+[A-Za-z]+\.[a-z]+"#
print(regex1)
print(regex2)



public enum Result<Success, Failure: Error> {
    case success(Success), failure(Failure)
}

let url: URLRequest = URLRequest(url: URL(string: "www.baidu.com")!)
URLSession.shared.dataTask(with: url) { data, response, error in
    
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
    guard error != nil else {
        handleError(error!)
        return
    }
    
    guard let data = data, let response = response else { return /* Impossible? */ }
    
    handleResponse(response, data: data)
}
func handleError(_ error: Error){
    
}
func handleResponse(_ res: URLResponse, data:Data){
    
}

// 定义方式
struct RandomNumberGenerator {
    func generate(numberOfZeroes: Int) -> Double {
        let maximum = pow(10, Double(numberOfZeroes))
        return Double.random(in: 0...maximum)
    }
}

// 调用方式
let random = RandomNumberGenerator()
let num = random.generate(numberOfZeroes: 2)


// 定义方式
@dynamicCallable
struct NewRandomNumberGenerator {
    func dynamicallyCall(withArguments args: [Int]) -> Double {
        let numberOfZeroes = Double(args.first ?? 0)
        let maximum = pow(10, numberOfZeroes)
        return Double.random(in: 0...maximum)
    }
}

// 调用方式
let newRandom = NewRandomNumberGenerator()
let num1 = newRandom(2) // random(2)等同于random.dynamicallyCall(withArguments: [2])


//可选值嵌套: 多层可选现在只返回一层可选
struct User {
    var id: Int
    
    init?(id: Int) {
        if id < 1 {
            return nil
        }
        
        self.id = id
    }
    
    func getMessages() throws -> String {
        // complicated code here
        return "No messages"
    }
}

let user = User(id: 1)
let messages = try? user?.getMessages()


let rowNumber = 4

if rowNumber.isMultiple(of: 2) {
    print("Even")
} else {
    print("Odd")
}


//过滤数组
let arr = [1, 2, 34, 5, 6, 7, 8, 12, 45, 6, 9]

let filter = arr.filter({ $0 > 10 })
print(filter)  // [34, 12, 45]

let count = arr.count(where: { $0 > 10 })
print(count)   // 3


//过滤字典

let times = [
    "first": 2,
    "second": 43,
    "three": 12,
    "four": 3,
]

let compact = times.compactMap({ $0.value > 10 })
// [true, false, true, false]

let mapValues = times.mapValues({ $0 + 2 })
// ["second": 45, "first": 4, "three": 14, "four": 5]
